package com.jiawa.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.train.business.domain.TrainCarriage;
import com.jiawa.train.business.domain.TrainCarriageExample;
import com.jiawa.train.business.enums.SeatColEnum;
import com.jiawa.train.business.mapper.TrainCarriageMapper;
import com.jiawa.train.business.req.TrainCarriageQueryReq;
import com.jiawa.train.business.req.TrainCarriageSaveReq;
import com.jiawa.train.common.exception.BusinessException;
import com.jiawa.train.common.exception.BusinessExceptionEnum;
import com.jiawa.train.common.resp.PageResult;
import com.jiawa.train.common.util.SnowUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TrainCarriageService {
    //reource是使用名称注入的方式而autowired是使用类型注入的方式
    @Resource
    private TrainCarriageMapper trainCarriageMapper;


    //新增或者更新用户信息，通过判断是否有id决定进行新增操作还是更新操作
    public void save(TrainCarriageSaveReq req) {
        //将传递的参数赋给TrainCarriage对象,在req中没有id,createTime,updateTime的数据，需要自己进行赋值
        //这里的id使用雪花算法，然后创建时间使用当前时间
        DateTime now = DateTime.now();

        //在保存的时候自动计算座位数和列数
        List seatColEnums = SeatColEnum.getColsByType(req.getSeatType());
        req.setColCount(seatColEnums.size());
        req.setSeatCount(req.getRowCount() * req.getColCount());
        TrainCarriage trainCarriage = BeanUtil.copyProperties(req, TrainCarriage.class);
        //如果id为空，则进行新增操作
        if(ObjectUtil.isNull(trainCarriage.getId())){
            TrainCarriageExample trainCarriageExample = new TrainCarriageExample();
            TrainCarriageExample.Criteria criteria = trainCarriageExample.createCriteria();
            criteria.andTrainCodeEqualTo(req.getTrainCode());
            criteria.andIndexEqualTo(req.getIndex());
            if(trainCarriageMapper.countByExample(trainCarriageExample) > 0){
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_CARRIAGE_NAME_UNIQUE_ERROR);
            }
            trainCarriage.setId(SnowUtil.getSnowflakeNextId());
            trainCarriage.setCreateTime(now);
            trainCarriage.setUpdateTime(now);
            trainCarriageMapper.insert(trainCarriage);
        }
        //否则进行更新操作
        else{
            trainCarriage.setUpdateTime(now);
            trainCarriageMapper.updateByPrimaryKeySelective( trainCarriage);
        }
    }

    //如果用户id存在则进行根据id进行查询乘客的信息，如果是用户id不存在则进行查询所有,全部都按分页进行处理
    public PageResult queryTrainCarriageList(TrainCarriageQueryReq  req) {
        //创建trainCarriageExample对象
        TrainCarriageExample trainCarriageExample = new TrainCarriageExample();
        trainCarriageExample.setOrderByClause("train_code asc,`index` asc");
        //设置查询条件
        TrainCarriageExample.Criteria criteria = trainCarriageExample.createCriteria();

        if(req.getTrainCode() != "" && req.getTrainCode() != null){
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }
        // 先设置分页的参数
        PageHelper.startPage(req.getPage(),req.getPageSize());
        //进行查询数据库
        List<TrainCarriage> trainCarriageList = trainCarriageMapper.selectByExample(trainCarriageExample);
        //返回查询结果,转为分页的形式,
        PageInfo<TrainCarriage> pageInfo = new PageInfo<>(trainCarriageList);
        //获取数据进行封装
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        //返回查询结果
        return pageResult;
    }

    public void delete(Long id) {
        trainCarriageMapper.deleteByPrimaryKey(id);
    }

    //根据车次查询车厢信息并进行返回数据
    public List queryListByTrainCode(String trainCode) {
        TrainCarriageExample trainCarriageExample = new TrainCarriageExample();
        trainCarriageExample.setOrderByClause("`index` asc");
        TrainCarriageExample.Criteria criteria = trainCarriageExample.createCriteria();
        criteria.andTrainCodeEqualTo(trainCode);
        List trainCarriageList = trainCarriageMapper.selectByExample(trainCarriageExample);
        return trainCarriageList;
    }

    public List<TrainCarriage> selectByTrainCode(String trainCode) {
        TrainCarriageExample trainCarriageExample = new TrainCarriageExample();
        TrainCarriageExample.Criteria criteria = trainCarriageExample.createCriteria();
        criteria.andTrainCodeEqualTo(trainCode);
        return trainCarriageMapper.selectByExample(trainCarriageExample);
    }
}